//
//  UIButton+SGExtension.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/11/1.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import UIKit

//extension UIButton: SpadgerCompatible {}

public extension Spadger where Base == UIButton {
    
    /// 设置图片位置
    /// - Parameters:
    ///   - imagePlacement: 图片位置
    ///   - imagePadding: 图片与文字的间距
    ///   - contentInsets: 内容的内边距
    func setImagePlacement(_ placement: NSDirectionalRectEdge, _ padding: CGFloat = 0, contentInsets: NSDirectionalEdgeInsets = .zero) {
        // 创建UIButtonConfiguration
        var config = UIButton.Configuration.plain()
        // 设置图像和图像位置
        config.image = base.currentImage
        // 图像放置的位置
        config.imagePlacement = placement
        // 可选：设置图像与文字之间的间距
        config.imagePadding = padding
        // 可选：设置内容的内边距
        config.contentInsets = contentInsets
        // 配置背景图片
        config.background.image = base.currentBackgroundImage
        // 应用配置到按钮
        base.configuration = config
    }
    
    /// 设置按钮的背景色
    /// - Parameters:
    ///   - color: 背景色
    ///   - state: 状态
    func setBackgroundColor(_ color: UIColor, for state: UIControl.State) {
        base.setBackgroundImage(color.toImage(), for: state)
    }
}
// MARK: - UIButton 图片 与 title 位置关系
public extension Spadger where Base: UIButton {
    
    /// 图片的方位
    enum ImagePosition {
        /// 图上，文字下
        case top
        /// 图左，文字右
        case left
        /// 图下，文字上
        case bottom
        /// 图右，文字左
        case right
    }
    
    /// 设置图片的方位
    /// - Parameters:
    ///   - position: 图片的位置
    ///   - padding: 图片和文字的间距
    @discardableResult
    func setImagePosition(_ position: ImagePosition = .left, padding: CGFloat = 0) -> UIButton {
        switch position {
        case .left:
            alignHorizontal(spacing: padding, imageFirst: true)
        case .right:
            alignHorizontal(spacing: padding, imageFirst: false)
        case .top:
            alignVertical(spacing: padding, imageTop: true)
        case .bottom:
            alignVertical(spacing: padding, imageTop: false)
        }
        return self.base
    }
    
    /// 水平方向
    /// - Parameters:
    ///   - spacing: 间距
    ///   - imageFirst: 图片是否优先
    private func alignHorizontal(spacing: CGFloat, imageFirst: Bool) {
        let edgeOffset = spacing / 2
        base.imageEdgeInsets = UIEdgeInsets(
            top: 0,
            left: -edgeOffset,
            bottom: 0,
            right: edgeOffset
        )
        base.titleEdgeInsets = UIEdgeInsets(
            top: 0,
            left: edgeOffset,
            bottom: 0,
            right: -edgeOffset
        )
        if !imageFirst {
            base.transform = CGAffineTransform(scaleX: -1, y: 1)
            base.imageView?.transform = CGAffineTransform(scaleX: -1, y: 1)
            base.titleLabel?.transform = CGAffineTransform(scaleX: -1, y: 1)
        }
        base.contentEdgeInsets = UIEdgeInsets(top: 0, left: edgeOffset, bottom: 0, right: edgeOffset)
    }
    
    /// 垂直方向
    /// - Parameters:
    ///   - spacing: 间距
    ///   - imageTop: 图片是不是在顶部
    private func alignVertical(spacing: CGFloat, imageTop: Bool) {
        guard let imageSize = self.base.imageView?.image?.size,
              let text = self.base.titleLabel?.text,
              let font = self.base.titleLabel?.font
        else {
            return
        }
        let labelString = NSString(string: text)
        let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: font])
        
        let imageVerticalOffset = (titleSize.height + spacing) / 2
        let titleVerticalOffset = (imageSize.height + spacing) / 2
        let imageHorizontalOffset = (titleSize.width) / 2
        let titleHorizontalOffset = (imageSize.width) / 2
        let sign: CGFloat = imageTop ? 1 : -1
        
        base.imageEdgeInsets = UIEdgeInsets(
            top: -imageVerticalOffset * sign,
            left: imageHorizontalOffset,
            bottom: imageVerticalOffset * sign,
            right: -imageHorizontalOffset
        )
        base.titleEdgeInsets = UIEdgeInsets(
            top: titleVerticalOffset * sign,
            left: -titleHorizontalOffset,
            bottom: -titleVerticalOffset * sign,
            right: titleHorizontalOffset
        )
        // increase content height to avoid clipping
        let edgeOffset = (min(imageSize.height, titleSize.height) + spacing) / 2
        base.contentEdgeInsets = UIEdgeInsets(top: edgeOffset, left: 0, bottom: edgeOffset, right: 0)
    }
}

public extension UIButton {
    
    // MARK: 2.1、设置title
    /// 设置title
    /// - Parameters:
    ///   - text: 文字
    ///   - state: 状态
    /// - Returns: 返回自身
    @discardableResult
    func yltitle(_ text: String?, _ state: UIControl.State = .normal) -> Self {
        setTitle(text, for: state)
        return self
    }
}
